﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using global::HolisticWare.VoIP.Getafisk.DLL_01DA_netfx35.any_any_any;
using global::HolisticWare.VoIP.Getafisk.DLL_01DA_netfx35.any_any_any.FreePBX;

namespace HolisticWare.VoIP.Getafisk.DLL_02BL_netfx35
{
	public
		partial
		class 
		Extension
	{

		public
			Extension
			(
			 Sip extension_sip
			)
		{
		}

		public
			static
			List<Extension>
			Extensions
			(
			)
		{
			List<Extension> extensions = new List<Extension>();

			DataBase db = new DataBase();
			List<Sip> extensions_sip = db.ExtensionsSIP();
			List<string> extension_numbers_only =
					(	
						from sip in extensions_sip
						where (sip.ID != null || sip.ID != "")
						select sip.ID
					)
					.Distinct()
					.ToList<string>()
					;

			foreach (string ext_str in extension_numbers_only)
			{
				List<Sip> extension_parameters_key_value =
					(
						from esp in extensions_sip
						where (esp.ID == ext_str)
						select esp
					).ToList<Sip>()
					;

				ExtensionSIP e = new ExtensionSIP();
				e.ExtensionNumber = Int32.Parse(ext_str);

				foreach (Sip es in extension_parameters_key_value)
				{
					string data = es.Data;
					string key = es.Keyword;
					string yn;
					switch (es.Keyword)
					{
						case "secret":
							if ("" == data)
							{
								// Nullable??
								e.Secret = Int32.MinValue;
							}
							else
							{
								e.Secret = Int32.Parse(data);
							}

							break;
						case "dtmfmode":
							//e.D = es.Data;
							break;
						case "canreinvite":
							yn = es.Data.ToLower();
							if ("yes" == yn)
							{
								e.CanReinvite = true;
							}
							else if ("no" == yn)
							{
								e.CanReinvite = false;
							}
							else
							{
								throw new Exception();
							}
							break;
						case "context":
							e.Context = es.Data;
							break;
						case "host":
							e.Host = es.Data;
							break;
						case "type":
							//e.Type = es.Data;
							break;
						case "nat":
							yn = es.Data.ToLower();
							if ("yes" == yn)
							{
								e.Nat = true;
							}
							else if ("no" == yn)
							{
								e.Nat = false;
							}
							else
							{
								throw new Exception();
							}
							break;
						case "port":
							break;
						case "qualify":
							break;
						case "callgroup":
							break;
						case "pickupgroup":
							break;
						case "disallow":
							break;
						case "allow":
							break;
						case "dial":
							break;
						case "accountcode":
							break;
						case "mailbox":
							break;
						case "deny":
							break;
						case "permit":
							break;
						case "account":
							break;
						case "callerid":
							break;
						case "record_in":
							break;
						case "record_out":
							break;
						default:
							break;
					} // switch
				} // foreach
				extensions.Add(e);
			}
			return extensions;
		}

	}
}
